﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using Bling.DSL;
using Bling.WPF;
using Bling.Animation;

namespace Bling.Tutorial {
  public class DistortTest : Canvas {
    public DistortTest() {
      CanvasBl canvas = this;
      PointBl ImageSize = new PointBl(Properties.Resources.Dock.Width, Properties.Resources.Dock.Height);
      var ratios = (canvas.Size - new PointBl(0, 40)) / ImageSize;
      var min = ratios.X.Min(ratios.Y);
      ImageSize = ImageSize * min;
      var image = new ImageBl(canvas) {
        ZIndex = 3, Source = Properties.Resources.Dock,
        CenterTop = new PointBl(canvas.CenterSize.X, 0),
        Size = ImageSize, IsHitTestVisible = false, Opacity = 0.2,
      };
      var imageB = new CanvasBl(canvas) {
        ZIndex = 1, Background = Properties.Resources.Dock,
        LeftTop = image.LeftTop,
        Size = image.Size,
      };
      Func<string, LabelBl> MkLabel = (nm) => {
        var ret = new LabelBl(imageB) {
          Content = nm, Font = { Weight = FontWeights.SemiBold },
        };
        ret.Font.Size = ret.IsMouseOver.Condition(40, 20);
        ret.Font.Brush = ret.IsMouseOver.Condition(Brushes.Red, Brushes.Black);
        return ret;
      };
      MkLabel("Center").CenterPosition = imageB.CenterSize;
      MkLabel("NorthWest").LeftTop = new PointBl(0,0);
      MkLabel("NorthEast").RightTop = new PointBl(imageB.Width,0);
      MkLabel("SouthWest").LeftBottom = new PointBl(0, imageB.Height);
      MkLabel("SouthEast").RightBottom = imageB.Size;



      PointBl[] points = new PointBl[4];
      PointBl[] orig = new PointBl[4];
      Brush[] brushes = new Brush[] {
        Brushes.Red, Brushes.Blue, Brushes.Green, Brushes.Purple,
      };
      PointBl[] initial = new PointBl[] {
        new PointBl(0,0), new PointBl(1,0), new PointBl(1,1), new PointBl(0,1)
      };

      for (int i = 0; i < 4; i++) {
        var q = new ThumbBl(canvas) {
          ZIndex = 3, Background = brushes[i],
          Map = (self, p) => p.Clamp(imageB.LeftTop - self.Size / 2, imageB.RightBottom - self.Size / 2), CanDrag = true,
          CenterPosition = imageB.LeftTop + initial[i] * imageB.Size,
        }.CenterPosition;
        orig[i] = q;
        points[i] = (q - imageB.LeftTop) / imageB.Size;
      }
      var center = new ThumbBl(canvas) {
        ZIndex = 3, Background = Brushes.Orange,
        CenterPosition = (orig[0] + orig[1] + orig[2] + orig[3]) / 4d,
      };
      for (int i = 0; i < 4; i++) {
        new LineBl(canvas) {
          Start = orig[i], End = orig[(i + 1) % 4], ZIndex = 2, Stroke = { Brush = Brushes.Black, Thickness = 2 },
        };
        new LineBl(canvas) {
          Start = orig[i], End = center, ZIndex = 2, Stroke = { Brush = Brushes.Orange, Thickness = 2 },
        };
      }
      var area = points[0].TriangleArea(points[1], points[2]) +
        points[0].TriangleArea(points[2], points[3]);

      var slider = new SliderBl(canvas) {
        Minimum = 0, Maximum = 1, Value = 1, ZIndex = 3, RightBottom = canvas.Size, Width = canvas.Width,
      };

      imageB.Effect.CustomWithMap = (input, uv) => {
        BoolBl isIn;
        var uv0 = uv.Distort(area, out isIn, slider.Value, points);
        //return input[uv];
        return isIn.Condition(input[uv0], Colors.Transparent);
      };

    }
  }

}
